package servlet_stanza;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import dao_casa.GestoreCasa;
import dao_stanza.GestoreStanza;

/**
 * Servlet implementation class configuraStanza
 */
@WebServlet("/configuraStanza")
public class configuraStanza extends HttpServlet {
	private static final long serialVersionUID = 1L;
     private  Integer count=0; 
     //totale dei metri quadri delle stanze aggiunte
     private Float totalMq=0f;
     private bean.Casa casa;
     private GestoreStanza gestStanza=new GestoreStanza();
     private HttpSession session;
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		RequestDispatcher rd;
		Float mq=Float.parseFloat(request.getParameter("mq").replace(",", "."));
		String categoria=request.getParameter("tipoStanza");
		
		if(count==0){
			session=request.getSession(true);
			casa=(bean.Casa)session.getAttribute("casa");
			GestoreCasa gestore=new GestoreCasa();
			gestore.persist(casa);
			}
		
		bean.Stanza stanza=new bean.Stanza(mq, categoria, casa);
		this.gestStanza.persist(stanza);
		this.count++;
		totalMq=totalMq+mq;
		request.setAttribute("count", this.count.toString());
		request.setAttribute("totalMq", this.totalMq.toString());
		rd=request.getRequestDispatcher("/configurazioneStanza.jsp");
		rd.forward(request, response);
	}
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
		this.session.removeAttribute("casa");
		this.count=0;
		this.totalMq=0f;
		response.sendRedirect("configurazioneCasaCompletata.html");
		
		
	}

	

}
